package com.ficus.road.maintain.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ficus.road.maintain.core.model.*;
import com.ficus.road.maintain.core.model.zh.*;
import com.ficus.road.maintain.core.model.zh.bridgehotel.BridgeHotelVO;
import com.ficus.road.maintain.core.model.zh.maintenance.MaintenanceTmOut;
import com.ficus.road.maintain.core.model.zh.roadice.RoadIceVO;
import com.ficus.road.maintain.core.model.zh.roadice.RoadIceZH;
import com.ficus.road.maintain.dao.*;
import com.ficus.road.maintain.service.BridgeInfoService;
import com.ficus.road.maintain.service.PanelService;
import com.ficus.road.maintain.service.ZhongHaiService;
import com.ficus.road.maintain.service.ZhonghaiClient;
import com.ficus.road.maintain.third.zh.roadice.ZHRoadIceInspection;
import com.ficus.road.maintain.util.MileageUtil;
import com.ficus.road.maintain.util.MyDateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import java.util.*;
import java.util.stream.Collectors;

import static com.ficus.road.maintain.util.SoapUtil.*;

/**
 * @author xiawen yang
 * @date 2021/10/15 下午8:22
 */
@RestController
@RequestMapping("/zhonghai")
@Slf4j
public class ZhongHaiController {
    @Autowired
    private ZhongHaiService zhongHaiService;
    @Autowired
    private ZhonghaiClient zhonghaiClient;
    @Autowired
    private RoadIceDao roadIceDao;
    @Autowired
    private MechanicalCleaningDao mechanicalCleaningDao;
    @Autowired
    private OverloadMonitorDao overloadMonitorDao;
    @Autowired
    private HitAllFactoryDao hitAllFactoryDao;
    @Autowired
    private BridgeInfoDao bridgeInfoDao;
    @Autowired
    private BridgeInfoService bridgeInfoService;
    @Autowired
    private MileageInfoDao mileageInfoDao;
    @Autowired
    private MaintenanceTmOutDao maintenanceTmOutDao;
    @Autowired
    private RoadIntactDao roadIntactDao;
    @Autowired
    private PanelService panelService;

    //道路完好率高速公路展示路段
    @Autowired
    ShowRoadIntactDao showRoadIntactDao;

    @Autowired
    ZHRoadIceInspection zhRoadIceInspection;

    private static List<String> roadList = new ArrayList<>();
    //道路完好率城市道路展示路段
    @Autowired
    ShowRoadIntactDirectDao showRoadIntactDirectDao;
    private static List<String> directList = new ArrayList<>();

    @PostMapping("/query")
    public ResponseEntity<String> queryZhongHaiData(@RequestBody ZhongHai zhongHai) {
        String zhongHaiData = zhongHaiService.getZhongHaiData(zhongHai);
        String jsonContent = null;
        PanelConfig panelConfig = panelService.getPanelConfig();
        try {
            jsonContent = extractJsonFromSoap(zhongHaiData);
            // 修改 JSON 内容
            String modifiedJson = modifyJson(jsonContent, "机械保洁率", panelConfig.getRoadCleaningRate());

            // 更新原始 SOAP XML
            zhongHaiData = replaceJsonInSoap(zhongHaiData, modifiedJson);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity<>(zhongHaiData, HttpStatus.OK);
        }

        return new ResponseEntity<>(zhongHaiData, HttpStatus.OK);
    }

    @PostConstruct
    @Scheduled(cron = "0 1 6-20 * * ?", zone = "GMT+8:00")
    public void getRoadList() {
        List<ShowRoadIntact.Road> all = showRoadIntactDao.findAll();
        List<String> roads = new ArrayList<>();

        roads.addAll(all.get(0).getAllRoad());
        roadList = roads;
    }

    @PostConstruct
    @Scheduled(cron = "0 1 6-20 * * ?", zone = "GMT+8:00")
    public void getDirectList() {
        List<ShowRoadIntact.Direct> all = showRoadIntactDirectDao.findAll();
        List<String> directs = new ArrayList<>();
        for (ShowRoadIntact.Direct direct : all) {
            directs.add(direct.getUnitName());
        }
        directList = directs;
    }

    /**
     * 桥孔异常基础数据
     *
     * @return
     */
    @GetMapping("/bridgeHotel/query")
//    @PreAuthorize("@rm.check('THIRD_PARTY_ZH')")
    public ResponseEntity<List<BridgeHotelVO>> getBridgeHotelInfo() {
        List<BridgeHotelVO> bridgeHotelInfo = zhongHaiService.getBridgeHotelInfo();
        return new ResponseEntity<>(bridgeHotelInfo, HttpStatus.OK);
    }

    /**
     * 桥孔异常告警数据
     *
     * @return
     */
    @GetMapping("/bridgeHotelWarning/query")
//    @PreAuthorize("@rm.check('THIRD_PARTY_ZH')")
    public ResponseEntity<List<WarningDetail>> getBridgeHotelWarning() {
        List<WarningDetail> bridgeHotelWarning = zhongHaiService.getBridgeHotelWarning();
        return new ResponseEntity<>(bridgeHotelWarning, HttpStatus.OK);
    }

    /**
     * 路面结冰
     *
     * @return
     */
    @GetMapping("/roadIce/query")
//    @PreAuthorize("@rm.check('THIRD_PARTY_ZH')")
    public ResponseEntity<List<RoadIceVO>> getRoadIceInfo() {

        List<RoadIceVO> roadIceInfo = zhongHaiService.getRoadIceInfo();
//        List<RoadIce> all = roadIceDao.findAll();
        return new ResponseEntity<>(roadIceInfo, HttpStatus.OK);
    }

    @GetMapping("/roadIce/create")
//    @PreAuthorize("@rm.check('THIRD_PARTY_ZH')")
    public ResponseEntity createIceInfo() {
        roadIceDao.deleteAll();
//        zhRoadIceInspection.refreshRoadIceInspection();
        String jsonStr="[{\"datatime\":\"2024-12-18T09:04:41\",\"deviceid\":\"SHA14RFM0000020\",\"devicename\":\"华洲路（迎宾下穿通道）\",\"lat\":\"31.19059951\",\"lon\":\"121.7717339\",\"runningstate\":\"11.1\",\"runningstate2\":\"0\",\"runningstate3\":\"0\",\"runningstate4\":\"0\",\"runningstate5\":\"0\",\"runningstate6\":\"0\",\"runningstate7\":\"0\",\"runningstate8\":\"0\",\"runningstate9\":\"0\"},{\"datatime\":\"2024-12-18T09:24:52\",\"deviceid\":\"SHA14RFM0000021\",\"devicename\":\"航城路（G1501下穿通道）\",\"lat\":\"31.15675801\",\"lon\":\"121.7657427\",\"runningstate\":\"12.6\",\"runningstate2\":\"0\",\"runningstate3\":\"0\",\"runningstate4\":\"0\",\"runningstate5\":\"0\",\"runningstate6\":\"0\",\"runningstate7\":\"0\",\"runningstate8\":\"0\",\"runningstate9\":\"0\"},{\"datatime\":\"2024-12-18T09:06:07\",\"deviceid\":\"SHA14RFM0000032\",\"devicename\":\"S20外环高速（杨高南路立交）\",\"lat\":\"31.13851377\",\"lon\":\"121.5365518\",\"runningstate\":\"9.7\",\"runningstate2\":\"0\",\"runningstate3\":\"0\"},{\"datatime\":\"2024-12-18T09:11:53\",\"deviceid\":\"SHA14RFM0000017\",\"devicename\":\"S20外环高速（康悟路）\",\"lat\":\"31.18853564\",\"lon\":\"121.7663458\",\"runningstate\":\"12.9\",\"runningstate2\":\"0\",\"runningstate3\":\"0\"},{\"datatime\":\"2024-12-18T09:06:07\",\"deviceid\":\"SHA14RFM0000033\",\"devicename\":\"S20外环高速（上南路跨线桥）\",\"lat\":\"31.14455614\",\"lon\":\"121.5674663\",\"runningstate\":\"9.7\",\"runningstate2\":\"0\",\"runningstate3\":\"0\",\"runningstate4\":\"0\",\"runningstate5\":\"0\",\"runningstate6\":\"0\",\"runningstate7\":\"0\",\"runningstate8\":\"0\",\"runningstate9\":\"0\"},{\"datatime\":\"2024-12-18T09:30:16\",\"deviceid\":\"SHA14RFM0000034\",\"devicename\":\"中环申江路立交监测点\",\"lat\":\"31.1830569\",\"lon\":\"121.6232313\",\"runningstate\":\"13.4\",\"runningstate2\":\"0\",\"runningstate3\":\"0\",\"runningstate4\":\"0\",\"runningstate5\":\"0\",\"runningstate6\":\"0\",\"runningstate7\":\"0\",\"runningstate8\":\"0\",\"runningstate9\":\"0\"},{\"datatime\":\"2024-12-18T09:30:11\",\"deviceid\":\"SHA14RFM0000035\",\"devicename\":\"中环济阳路立交监测点\",\"lat\":\"31.14810109\",\"lon\":\"121.4877668\",\"runningstate\":\"12.5\",\"runningstate2\":\"0\",\"runningstate3\":\"0\",\"runningstate4\":\"0\",\"runningstate5\":\"0\",\"runningstate6\":\"0\",\"runningstate7\":\"0\",\"runningstate8\":\"0\",\"runningstate9\":\"0\"},{\"datatime\":\"2024-12-18T09:30:05\",\"deviceid\":\"SHA14RFM0000036\",\"devicename\":\"中环杨高监测点\",\"lat\":\"31.15354834\",\"lon\":\"121.5274257\",\"runningstate\":\"10.9\",\"runningstate2\":\"0\",\"runningstate3\":\"0\",\"runningstate4\":\"0\",\"runningstate5\":\"0\",\"runningstate6\":\"0\",\"runningstate7\":\"0\",\"runningstate8\":\"0\",\"runningstate9\":\"0\"},{\"datatime\":\"2024-12-18T09:30:26\",\"deviceid\":\"SHA14RFM0000037\",\"devicename\":\"华夏高架环东立交监测点\",\"lat\":\"31.18935769\",\"lon\":\"121.6455215\",\"runningstate\":\"9.3\",\"runningstate2\":\"0\",\"runningstate3\":\"0\",\"runningstate4\":\"0\",\"runningstate5\":\"0\",\"runningstate6\":\"0\",\"runningstate7\":\"0\",\"runningstate8\":\"0\",\"runningstate9\":\"0\"},{\"datatime\":\"2024-12-18T09:30:21\",\"deviceid\":\"SHA14RFM0000038\",\"devicename\":\"S1华夏高架立交监测点\",\"lat\":\"31.18818357\",\"lon\":\"121.7719687\",\"runningstate\":\"11.7\",\"runningstate2\":\"0\",\"runningstate3\":\"0\",\"runningstate4\":\"0\",\"runningstate5\":\"0\",\"runningstate6\":\"0\",\"runningstate7\":\"0\",\"runningstate8\":\"0\",\"runningstate9\":\"0\"}]";
        List<RoadIceZH> roadIceZHList = JSONArray.parseArray(jsonStr, RoadIceZH.class);
        System.out.println(roadIceZHList.size());
        Date today = new Date();
        roadIceZHList.forEach(zh -> {
            String time = MyDateUtil.dateFormatStr(MyDateUtil.strParseDate(zh.getDatatime(), "yyyy-MM-dd'T'HH:mm:ss"), "yyyy-MM-dd HH:mm:ss");
            String updateTime = MyDateUtil.dateFormatStr(today, "yyyy-MM-dd HH:mm:ss");
            System.out.println(JSONObject.toJSONString(zh));
            RoadIce roadIce = zh.convert2RoadIce(zh, time, updateTime);
            roadIceDao.saveOrUpdate(roadIce);
        });
        return new ResponseEntity(HttpStatus.OK);
    }


    /**
     * 路面结冰
     *
     * @return
     */
    @GetMapping("/roadIceProcess/query")
//    @PreAuthorize("@rm.check('THIRD_PARTY_ZH')")
    public ResponseEntity<List<Hit>> getRoadIceProcessInfo() {

        List<Hit> hits = hitAllFactoryDao.findByQuery(new Query(Criteria.where("type").is(AllFactoryTypeEnum.ROAD_ICE)));
        return new ResponseEntity<>(hits, HttpStatus.OK);
    }


    /**
     * 超限超载
     *
     * @return
     */
    @GetMapping("/overrunCarMonitor/query")
//    @PreAuthorize("@rm.check('THIRD_PARTY_ZH')")
    public ResponseEntity<List<OverloadMonitor>> getOverloadMonitor() {
        List<OverloadMonitor> all = overloadMonitorDao.findAll();
        return new ResponseEntity<>(all, HttpStatus.OK);
    }

    /**
     * 超限超载数目
     *
     * @return
     */
    @GetMapping("/overrunCarMonitor/query/sum")
//    @PreAuthorize("@rm.check('THIRD_PARTY_ZH')")
    public Integer getOverloadMonitorSum() {
        List<OverloadMonitor> all = overloadMonitorDao.findAll();
        return all.size();
    }

    /**
     * 非法掘路
     *
     * @return
     */
    @GetMapping("/illegalDigging/query")
//    @PreAuthorize("@rm.check('THIRD_PARTY_ZH')")
    public ResponseEntity<List<IllegalDiggingResponse>> getIllegalDigging() {
        List<Hit> hits = hitAllFactoryDao.findByQuery(new Query(Criteria.where("type").is(AllFactoryTypeEnum.ILLEGAL_DIGGING)));
        List<IllegalDiggingResponse> res = new ArrayList<>();
        hits.forEach(hit -> {
            if (hit.getAttrs().getSource().equals("MOCK")) {
                return;
            }
            IllegalDiggingResponse response = IllegalDiggingResponse.builder()
                    .id(hit.getId())
                    .roadName(hit.getAttrs().getRoadName())
                    .unit(hit.getAttrs().getSection())
                    .company(hit.getAttrs().getHandleCompany())
                    .desc(hit.getAttrs().getDescription())
                    .coordinate(Coordinate.builder().x(hit.getAttrs().getGpsLocation().getLongitude() + "")
                            .y(hit.getAttrs().getGpsLocation().getLatitude() + "").build())
                    .status("0")
                    .build();
            res.add(response);
        });
        return new ResponseEntity<>(res, HttpStatus.OK);
    }

    /**
     * 维修超时
     *
     * @return
     */
    @GetMapping("/maintenanceTmOut/query")
//    @PreAuthorize("@rm.check('THIRD_PARTY_ZH')")
    public ResponseEntity<List<MaintenanceTmOut>> maintenanceTmOutQuery() {
//        String date = MyDateUtil.dateFormatStr(new Date(), "yyyy-MM");
//        Query query = new Query(Criteria.where("date").is(date));

        List<MaintenanceTmOut> maintenanceTmOutList = maintenanceTmOutDao.findAll();
        List<MaintenanceTmOut> res = maintenanceTmOutList.stream().sorted(Comparator.comparing(MaintenanceTmOut::getStartTime).reversed()).collect(Collectors.toList());
        return new ResponseEntity<>(res, HttpStatus.OK);
    }

    /**
     * 维修超时当前一年内数量
     *
     * @return
     */
    @GetMapping("/maintenanceTmOut/query/sum")
//    @PreAuthorize("@rm.check('THIRD_PARTY_ZH')")
    public Integer maintenanceTmOutQuerySum() {
        String year = MyDateUtil.dateFormatStr(new Date(), "yyyy");
        Query query = new Query(Criteria.where("date").regex("^" + year + "-"));

        List<MaintenanceTmOut> maintenanceTmOutList = maintenanceTmOutDao.findByQuery(query);
        return maintenanceTmOutList.size();
    }

    /**
     * 桥梁完损率
     *
     * @return
     */
    @GetMapping("/bridgeIntact/query")
    public ResponseEntity<List<BridgeIntactVO>> queryBridgeIntact() {
        List<BridgeIntactVO> res = new ArrayList<>();
        bridgeInfoService.findAll().forEach(bridge -> {
            BridgeIntactVO vo = BridgeIntactVO.builder()
                    .id(bridge.getId())
                    .bridge(bridge.getBridgeName())
                    .road(bridge.getRoadName())
                    .type(bridge.getBridgeType())
                    .underControl(bridge.getSecurity())
                    .unit(bridge.getUnit())
                    .company(bridge.getCompany())
                    .longitude(ObjectUtils.isEmpty(bridge.getLongitude()) ? null : Double.parseDouble(bridge.getLongitude()))
                    .latitude(ObjectUtils.isEmpty(bridge.getLatitude()) ? null : Double.parseDouble(bridge.getLatitude()))
                    .build();
            res.add(vo);
        });
        List<BridgeIntactVO> sortedResult = res.stream().sorted(Comparator.comparing(BridgeIntactVO::getCompany)).collect(Collectors.toList());
        return new ResponseEntity<>(sortedResult, HttpStatus.OK);
    }

    /**
     * 道路完好率
     *
     * @return
     */
    @GetMapping("/roadIntact/query")
//    @PreAuthorize("@rm.check('THIRD_PARTY_ZH')")
    public ResponseEntity<RoadIntactStatistic> roadIntactStatisticQuery() {
        RoadIntactStatistic res = new RoadIntactStatistic();
        int newHour = MyDateUtil.hour();
        String dateStr = "";
        if (newHour <= 20) {
            dateStr = MyDateUtil.GetBeforeDay(1);
        } else {
            dateStr = MyDateUtil.dateFormatStr(new Date(), "yyyy-MM-dd");
        }
        Query query = new Query(Criteria.where("date").is(dateStr));
        List<RoadIntactStatistic.RoadIntact> roadIntactList = roadIntactDao.findByQuery(query);
        int time = 2;
        if (roadIntactList.size() < 1) {
            while (roadIntactList.size() < 1) {
                String newDateStr = MyDateUtil.GetBeforeDay(time);
                Query newQuery = new Query(Criteria.where("date").is(newDateStr));
                roadIntactList = roadIntactDao.findByQuery(newQuery);
                time++;
            }
        }

        Map<RoadTypeEnum, List<RoadIntactStatistic.RoadIntact>> typeMap = roadIntactList.stream().filter(item -> ObjectUtils.isNotEmpty(item.getRoadType())).collect(Collectors.groupingBy(RoadIntactStatistic.RoadIntact::getRoadType));
        List<RoadIntactStatistic.RoadIntact> direct = typeMap.get(RoadTypeEnum.DIRECT);
        if (CollectionUtils.isEmpty(direct)) {
            return new ResponseEntity<>(res, HttpStatus.OK);
        }
        if (directList == null) {
            getDirectList();
        }
        //新建城市道路列表
        List<RoadIntactStatistic.RoadIntact> directlist = new ArrayList<>();
        //判断拿到的城市道路是否需要展示，需要则将其add进directlist
        for (RoadIntactStatistic.RoadIntact roadIntact : direct) {
            if (directList.contains(roadIntact.getUnitName())) {
                directlist.add(roadIntact);
            }
        }
        //对城市道路列表进行排序
        directlist.sort(Comparator.comparing(RoadIntactStatistic.RoadIntact::getRate).thenComparing(RoadIntactStatistic.RoadIntact::getCount));
        // 公路署
        res.setDirect(directlist);
        // 城道中心
        List<RoadIntactStatistic.RoadIntact> road = typeMap.get(RoadTypeEnum.HIGHWAY);
        List<ShowRoadIntact.Road> all = showRoadIntactDao.findAll();
        Set<String> allRoad = all.get(0).getAllRoad();
        ArrayList<RoadIntactStatistic.RoadIntact> roadlist = new ArrayList<>();
        for (RoadIntactStatistic.RoadIntact roadIntact : road) {
            if (allRoad.contains(roadIntact.getRoadId())) {
                roadlist.add(roadIntact);
            }
        }
        roadlist.sort(Comparator.comparing(RoadIntactStatistic.RoadIntact::getRate).thenComparing(RoadIntactStatistic.RoadIntact::getCount));
        res.setRoad(roadlist);
        return new ResponseEntity<>(res, HttpStatus.OK);
    }

    /**
     * 机械保洁率
     *
     * @param date
     * @return
     */
    @GetMapping("/mechanical/query/{date}")
//    @PreAuthorize("@rm.check('THIRD_PARTY_ZH')")
    public ResponseEntity<List<MechanicalCleaning>> getMechanicalCleaning(@PathVariable("date") String date) {
        Sort is_ok = new Sort(Sort.Direction.ASC, "is_ok");
        Query query = new Query(Criteria.where("date").is(date)).with(is_ok);
        List<MechanicalCleaning> all = mechanicalCleaningDao.findByQuery(query);
        List<MechanicalCleaning> sorted = all.stream().sorted(Comparator.comparing(MechanicalCleaning::getIsOk).reversed()).collect(Collectors.toList());
        return new ResponseEntity<>(sorted, HttpStatus.OK);
    }

    /**
     * 桥梁巡检率
     *
     * @return
     */
    @GetMapping("/bridgeInspection/query")
//    @PreAuthorize("@rm.check('THIRD_PARTY_ZH')")
    public ResponseEntity<List<BridgeInspectionResponse>> bridgeInspectionQuery() {
        Query query = new Query(Criteria.where("type").is(AllFactoryTypeEnum.BRIDGE_INSPECTION)
                .and("attrs.status").ne(HitStatusEnum.CONFIRMED));
        List<Hit> hitList = hitAllFactoryDao.findByQuery(query);
        Set<String> bridgeIdList = hitList.stream().map(hit -> hit.getAttrs().getDiseaseUid()).collect(Collectors.toSet());
        List<BridgeInfo> bridgeInfoList = bridgeInfoDao.findByQuery(new Query(Criteria.where("_id").in(bridgeIdList)));
        List<BridgeInspectionResponse> res = new ArrayList<>();
        bridgeInfoList.forEach(bridge -> {
            BridgeInspectionResponse response = BridgeInspectionResponse.builder()
                    .id(bridge.getId())
                    .bridge(bridge.getBridgeName())
                    .road(bridge.getRoadName())
                    .type(bridge.getBridgeType())
                    .company(bridge.getCompany())
                    .unit(bridge.getUnit())
                    .coordinate(Coordinate.builder()
                            .x(bridge.getLongitude())
                            .y(bridge.getLatitude())
                            .build())
                    .build();
            res.add(response);
        });
        return new ResponseEntity<>(res, HttpStatus.OK);
    }

    /**
     * 管养桥梁（统计数据和列表数据）
     *
     * @return
     */
    @GetMapping("/bridge/statistic")
    public ResponseEntity<BridgeStatistic> bridgeStatistic() {
        BridgeStatistic res = new BridgeStatistic();
        BridgeStatistic.Direct direct = new BridgeStatistic.Direct();
        BridgeStatistic.Road road = new BridgeStatistic.Road();
        BridgeStatistic.BridgeTypeStatistic bridgeTypeStatistic = new BridgeStatistic.BridgeTypeStatistic();
        BridgeStatistic.TechStatistic techStatistic = new BridgeStatistic.TechStatistic();
        direct.setBridgeTypeStatistic(bridgeTypeStatistic);
        direct.setTechStatistic(techStatistic);
        res.setDirect(direct);
        res.setRoad(road);

        List<BridgeInfo> bridgeInfoList = bridgeInfoDao.findAll();
        Map<RoadTypeEnum, List<BridgeInfo>> roadTypeBridgeMap = bridgeInfoList.stream().collect(Collectors.groupingBy(BridgeInfo::getRoadType));
        List<BridgeInfo> directList = roadTypeBridgeMap.get(RoadTypeEnum.DIRECT);
        Map<String, List<BridgeInfo>> bridgeTypeMap = directList.stream().collect(Collectors.groupingBy(BridgeInfo::getBridgeType));
        Map<String, List<BridgeInfo>> techLevelMap = directList.stream().filter(t -> t.getTechLevel() != null).collect(Collectors.groupingBy(BridgeInfo::getTechLevel));
        bridgeTypeMap.forEach((item, bridgeTypeList) -> {
            int size = bridgeTypeList.size();
            switch (item) {
                case "特大型":
                    bridgeTypeStatistic.setExtraLarge(size);
                    break;
                case "大型":
                    bridgeTypeStatistic.setLarge(size);
                    break;
                case "中型":
                    bridgeTypeStatistic.setMedium(size);
                    break;
                case "小型":
                    bridgeTypeStatistic.setSmall(size);
                    break;
                case "人行天桥":
                    bridgeTypeStatistic.setPedestrian(size);
                    break;
                default:
                    break;
            }
        });
        techLevelMap.forEach((item, bridgeTypeList) -> {
            int size = bridgeTypeList.size();
            switch (item) {
                case "一类桥":
                    techStatistic.setFirstType(size);
                    break;
                case "二类桥":
                    techStatistic.setSecondType(size);
                    break;
                case "三类桥":
                    techStatistic.setThirdType(size);
                    break;
                case "四类桥":
                    techStatistic.setFourthType(size);
                    break;
                default:
                    break;
            }
        });
        direct.setBridgeList(directList);
        direct.setTotal(directList.size());
        List<BridgeInfo> roadList = roadTypeBridgeMap.get(RoadTypeEnum.HIGHWAY);
        road.setBridgeList(roadList);
        road.setTotal(roadList.size());
        return new ResponseEntity<>(res, HttpStatus.OK);
    }

    /**
     * 管养里程（统计数据和列表数据）
     *
     * @return
     */
    @GetMapping("/mileage/statistic")
    public ResponseEntity<MileageStatistic> mileageStatistic() {
        Query query = new Query();
        query.with(new Sort(Sort.Direction.DESC, "_id")).skip(0).limit(1);
        MileageStatistic mileageStatistic = mileageInfoDao.findOne(query);
        // 对道路去重
        List<MileageStatistic.MileageBridge> directMileageList = mileageStatistic.getDirect().getMileageList();
        List<MileageStatistic.MileageBridge> roadMileageList = mileageStatistic.getRoad().getMileageList();
        // 转成set以驱虫
        Set<MileageStatistic.MileageBridge> directMileageSst = new HashSet<>(directMileageList);
        Set<MileageStatistic.MileageBridge> roadMileageSst = new HashSet<>(roadMileageList);
        // 赋值回参数中的list上
        ArrayList<MileageStatistic.MileageBridge> directMileageBridges = new ArrayList<>(directMileageSst);
        ArrayList<MileageStatistic.MileageBridge> roadMileageBridges = new ArrayList<>(roadMileageSst);
        MileageUtil comparator = new MileageUtil();
        Collections.sort(directMileageBridges, comparator);
        Collections.sort(roadMileageBridges, comparator);
        mileageStatistic.getDirect().setMileageList(directMileageBridges);
        mileageStatistic.getRoad().setMileageList(roadMileageBridges);
        return new ResponseEntity<>(mileageStatistic, HttpStatus.OK);
    }
}
